<template>
    <div class = 'login_container'>
      <div class="login_box">
        <div class="login_head">
          <img class ="login_img" src="../assets/logo.png">
        </div>
        <div>
            <el-form class="loginform" ref="loginFormRef" :rules="rules" :model="form">
              <el-form-item prop="username">
                <el-input placeholder="用户名" prefix-icon="el-icon-user-solid" v-model="form.username" ></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input placeholder="密码" type="password" prefix-icon="el-icon-s-goods" v-model="form.password"></el-input>
              </el-form-item>
              <el-form-item class="bts">
              <el-button type="success" @click="tijiao">登录</el-button>
              <el-button type="info" @click="resetForm">重置</el-button>
              </el-form-item>
            </el-form>
        </div>
      </div>
    </div>
</template>

<script>
export default {
  name: 'login',
  data () {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.loginFormRef.resetFields()
    },
    tijiao () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: result } = await this.$http.post('login', this.form)
        // console.log(result.meta)
        if (result.meta.status !== 200) return this.$message.error(result.meta.msg)
        window.sessionStorage.setItem('token', result.data.token)
        this.$message.success(result.meta.msg)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped>
.login_container{
  background-color: #2b4b6b;
  height: 100%;
}
  .login_box{
    width: 450px;
    height: 300px;
    background-color: white;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top:50%;
    transform: translate(-50%,-50%);
  }
.login_head {
  height: 130px;
  width: 130px;
  border: 1px solid #42b983;
  border-radius: 50%;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top:-25%;
  padding: 10px;
  transform: translate(-50%);
  box-shadow: 0 0 10px;
}
.login_img{
    height: 100%;
    width: 100%;
  border-radius: 50%;
  background-color: #ccc;
  }
  .bts{
    display: flex;
    justify-content: flex-end;
    padding-right: 10%;
  }
.loginform{
  position: absolute;
  bottom: 0;
  width: 80%;
  padding: 0 10%;
}
</style>
